package edu.ycxy.poetry.service.serviceImpl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.ChaoDai;
import edu.ycxy.poetry.mapper.ChaoDaiMapper;
import edu.ycxy.poetry.service.IChaoDaiService;
import edu.ycxy.poetry.vo.AddDynastyVo;
import edu.ycxy.poetry.vo.UpdateDynastyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@Service
public class ChaoDaiServiceImpl extends ServiceImpl<ChaoDaiMapper, ChaoDai> implements IChaoDaiService {
    @Autowired
    private ChaoDaiMapper chaoDaiMapper;

    //根据朝代名查询朝代的具体信息
    @Override
    public ChaoDai findDynastyByName(String name) {
        return chaoDaiMapper.findDynasty(name);
    }

    //查询所有朝代的名字
    @Override
    public List<ChaoDai> findDynastyName() {
        return chaoDaiMapper.findDynastyName();
    }

    // 添加朝代信息
    @Override
    public String addDynasty(AddDynastyVo addDynastyVo) {
        // 根据vo查询该朝代是否存在
        ChaoDai dynasty = chaoDaiMapper.findDynasty(addDynastyVo.getDynastyName());
        //当朝代不为空的时候,进行数据更新
        if (dynasty != null){
            chaoDaiMapper.updateDynasty(addDynastyVo.getDynastyName(),addDynastyVo.getTxt(),dynasty.getId());
            return "数据更新";
        }else {
            chaoDaiMapper.addNewDynasty(addDynastyVo.getDynastyName(),addDynastyVo.getTxt());
            return "朝代添加";
        }
    }


    // 模糊查询朝代
    @Override
    public PageInfo<ChaoDai> findListByNameToLike(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ChaoDai> listByNameToLike = chaoDaiMapper.findListByNameToLike(name);
        PageInfo pageInfo = new PageInfo(listByNameToLike);

        return pageInfo;
    }

    // 根据id删除朝代信息
    @Override
    public Integer deleteDynastyById(Integer id) {
        Integer integer = chaoDaiMapper.deleteDynastyById(id);
        if (integer == 1){
            return 1;
        }else {
            return 2;
        }
    }

    // 根据id查询朝代信息
    @Override
    public UpdateDynastyVo findOneById(Integer id) {
        UpdateDynastyVo oneById = chaoDaiMapper.findOneById(id);
        return oneById;
    }

    // 更新朝代信息
    @Override
    public Integer updateDynasty(UpdateDynastyVo updateDynastyVo) {
        Integer integer = chaoDaiMapper.updateDynasty(updateDynastyVo.getName(), updateDynastyVo.getTxt(), updateDynastyVo.getId());
        return integer;
    }
}
